package lab.mongo.service.impl;

import com.mongodb.client.result.UpdateResult;
import com.sun.imageio.plugins.common.SubImageInputStream;
import lab.mongo.pojo.*;
import lab.mongo.service.IMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class MongoServiceImpl implements IMongoService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void addSubjects(List<SubjectInfo> subjects) {
        mongoTemplate.insert(subjects, "subjectInfo");
    }

    @Override
    public UpdateResult updateSubject(SubjectInfo subject) {
        Query query = new Query(Criteria.where("subjectId").is(subject.getSubjectId()));
        Update update = new Update();
        update.set("amount", subject.getAmount());
        return mongoTemplate.updateFirst(query, update, "subjectInfo");
    }

    @Override
    public List<SubjectInfo> findSubject(PageInfo pageInfo, QuerySubject query) {
        Query mQuery = new Query();
        if (query.getSubjectCode() != null) {
            mQuery.addCriteria(Criteria.where("subjectCode").is(query.getSubjectCode()));
        }
        if (query.getSubjectFullName() != null) {
            String regex = String.format("%s%s%s", "^.*", query.getSubjectFullName(), ".*$");
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            mQuery.addCriteria(Criteria.where("subjectFullName").regex(pattern));
        }
        if (query.getSubjectCodes() != null) {
            mQuery.addCriteria(Criteria.where("subjectCode").in(query.getSubjectCodes()));
        }
        if (query.getCompanyId() != 0) {
            mQuery.addCriteria(Criteria.where("companyId").is(query.getCompanyId()));
        }
        if (query.getCreateTime() != null) {
            mQuery.addCriteria(Criteria.where("createTime").gte(query.getCreateTime()));
        }
        int offset = (pageInfo.getPage() - 1) * pageInfo.getPageSize();
        List<SubjectInfo> subjects = mongoTemplate.find(mQuery.skip(offset).limit(pageInfo.getPageSize()), SubjectInfo.class, "subjectInfo");

        return subjects;
    }

    @Override
    public void deleteSubjects(QuerySubject query) {
//        mongoTemplate.aggregate()
    }

    @Override
    public void addCompanys(List<CompanyInfo> companys) {
        mongoTemplate.insert(companys, "companyInfo");
    }

    @Override
    public List<CompanyInfo> findCompany(PageInfo pageInfo, QueryCompany query) {
        List<CompanyInfo> subjects = Collections.emptyList();
        if (query == null) {
            subjects = mongoTemplate.findAll(CompanyInfo.class);
        } else {
            subjects = mongoTemplate.find(new Query(), CompanyInfo.class, "subjectInfo");
        }
        return subjects;
    }
}
